package helper.request.pim;

import java.sql.SQLException;

import server.dbconnector.DatabaseConnector;
import base.User;

public class PIMRequest implements helper.request.RequestUndo{
	private static final long serialVersionUID = 7144267887638965454L;
	private User currentuser,preUser;
	private String email;
	private String password;
	private String realName;
	private String phone;
	private int Sex;
	public PIMRequest(User user,String realName,String email,int Sex,String password,String phone){
		this.currentuser=user;
		this.email=email;
		this.realName=realName;
		this.Sex=Sex;
		this.password=password;
		this.phone=phone;
		this.preUser=user.clone();
		user.setemail(email);
		user.setrealname(realName);
		user.setsex(Sex);
		user.setpassword(password);
		user.setphonenumber(phone);
		
	}

	@Override
	public User getcurrentuser() {
		return currentuser;
	}

	@Override
	public int getcurrentuserid() {
		// TODO Auto-generated method stub
		return currentuser.getid();
	}
	@Override
	public boolean execute() {
		try{
			currentuser.setemail(email);
			currentuser.setrealname(realName);
			currentuser.setsex(Sex);
			currentuser.setpassword(password);
			currentuser.setphonenumber(phone);
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
		return true;
	}
	public boolean de_execute(){
		currentuser.setemail(preUser.getemail());
		currentuser.setrealname(preUser.getrealname());
		currentuser.setsex(preUser.getsex());
		currentuser.setpassword(preUser.getpassword());
		currentuser.setphonenumber(preUser.getphonenumber());
		return true;
	}
	public String toString(){
		return "修改了个人信息";
	}

	@Override
	public void process(DatabaseConnector dc)throws SQLException{
		dc.commit("update user set password='"+this.password+"' where ID="+this.getcurrentuserid());
		dc.commit("update user set email='"+this.email+"' where ID="+this.getcurrentuserid());
		dc.commit("update user set phonenumber='"+this.phone+"' where ID="+this.getcurrentuserid());
		dc.commit("update user set sex="+this.Sex+" where ID="+this.getcurrentuserid());
		dc.commit("update user set realname='"+this.realName+"' where ID="+this.getcurrentuserid());
		
	}
}
